package com.lizardmind.haofudoctor.ui.diagnosis.sickMan

import android.os.Bundle
import android.view.View
import com.linxiao.framework.activity.BaseActivity
import com.linxiao.framework.util.color
import com.linxiao.framework.util.getBaseFragmentStatePagerAdapter
import com.linxiao.framework.util.getDataBinding
import com.linxiao.framework.widget.NavigationView
import com.lizardmind.haofudoctor.R
import com.lizardmind.haofudoctor.databinding.ActivitySickManBinding
import com.lizardmind.haofudoctor.manager.postApi
import com.lizardmind.haofudoctor.other.*
import com.lizardmind.haofudoctor.ui.diagnosis.diagnose.LookDiagnoseActivity
import com.lizardmind.haofudoctor.ui.diagnosis.diagnose.userFill.FillDiagnoseOneActivity
import com.lizardmind.haofudoctor.ui.diagnosis.sickMan.MedicalHistory.FangAnActivity
import com.lizardmind.haofudoctor.ui.diagnosis.sickMan.MedicalHistory.MedicalHistoryFragment
import com.lizardmind.haofudoctor.ui.diagnosis.visit.DiyVisitTimeActivity
import com.lizardmind.haofudoctor.ui.diagnosis.visit.LookVisitActivity
import com.lizardmind.haofudoctor.ui.diagnosis.visit.userFill.FillVisitActivity
import io.reactivex.functions.Consumer
import io.rong.imkit.fragment.ConversationFragment
import kotlinx.android.synthetic.main.include_title_text.*
import org.greenrobot.eventbus.Subscribe
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.support.v4.act
import org.jetbrains.anko.toast

/**
 * Created by Administrator on 2017/10/20.
 *诊疗服务——患者:咨询，病历
 */
class SickManActivity: BaseActivity() {

    private val listFragment= arrayListOf(ConversationFragment(), MedicalHistoryFragment())
    private val rUid by lazy{ intent.data.getQueryParameter("targetId") }

    val binding by lazy { getDataBinding<ActivitySickManBinding>(R.layout.activity_sick_man) }
    override fun onCreatRootView(): View {
        return  binding.root
    }

    override fun onInitView(savedInstanceState: Bundle?) {
        setEvent(true)
        binding.apply {
            VPagerSickMan.setScanScroll(false)
            VPagerSickMan.adapter= getBaseFragmentStatePagerAdapter(supportFragmentManager,listFragment)
            naViewSickMan.binding(VPagerSickMan)
                    .setTextViewChildTextFromString(resources.getStringArray(R.array.sick_man))
                    .setUnSelectTextColor(color(R.color.gray3))
                    .setSelectTextColor(color(R.color.notice_red))
                    .setTextSize(16f)//文字大小，单位为sp，默认为42px
                    .setSlidingStripPosition(NavigationView.BOTTOM)
                    .setScllorHeight(2f)
                    .setScllorColor(color(R.color.notice_red))
                    .commit()//提交，执行修改
        }

        binding.titleSickMan.apply {
            titleText.text=intent.data.getQueryParameter("title")
        }
        imgLeft.setImageResource(R.mipmap.back)
        imgLeft.setOnClickListener {
            finish()
        }
    }

    /**
     * 会话界面里问诊单的点击处理
     */
    @Subscribe
    fun onClickDiagnose(event: OnClickRongDiagnoseEvent){
        if(event.isSelf){//医生方向
            //医生不能点击自己发送的问诊单
            toast("不能点击自己发送的问诊单")
            //这是患者点击医生发送的问诊单,已测试
//            observe {
//                postApi<Request<DiagnoseDocumentsBean>>(hashMapOf(
//                        "action" to "diagnose_info",
//                        "token" to DoctorInfo.token,
//                        "id" to event.id
//                ), Consumer {
//                    it.dispose {
//                        startActivity<FillDiagnoseOneActivity>("bean" to it)
//                    }
//                },context = this)
//            }
        }else{//患者方向
            startActivity<LookDiagnoseActivity>("id" to event.id)
        }
    }

    /**
     * 会话界面里方案的点击处理
     */
    @Subscribe
    fun onClickFangan(event: OnClickRongFanganEvent){
        startActivity<FangAnActivity>("id" to event.id)
    }

    /**
     * 会话界面里复诊单的点击处理
     */
    @Subscribe
    fun onClickVisit(event: OnClickRongVisitEvent){
        if(event.isSelf){//医生方向
            //医生不能点击自己发送的问诊单
            toast("不能点击自己发送的复诊单")
            //这是患者点击医生发送的复诊单,已测试
//            startActivity<FillVisitActivity>("id" to event.id)
        }else{//患者方向
            startActivity<LookVisitActivity>("id" to event.id)
        }
    }

    /**
     * 会话界面里方案tips的点击处理
     */
    @Subscribe
    fun onClickTips(event: OnClickRongTipsEvent){
        observe {
            postApi<Request<PrescribingBean>>(hashMapOf("action" to "prescribing",
                    "token" to DoctorInfo.token,"id" to event.id),
                    Consumer {
                        it.dispose {
                            startActivity<DiyVisitTimeActivity>("pid" to it.id,
                                    "create_time" to it.create_time,
                                    "custom_time" to it.custom_time,
                                    "rUid" to rUid)
                        }
                    },context = this)
        }
    }
}